package cn.iocoder.yudao.module.member.controller.admin.userrefund;

import cn.iocoder.yudao.module.member.enums.userrefund.RefundStatusEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.userrefund.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userrefund.UserRefundDO;
import cn.iocoder.yudao.module.member.service.userrefund.UserRefundService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 退款")
@RestController
@RequestMapping("/member/user-refund")
@Validated
public class UserRefundController {

    @Resource
    private UserRefundService userRefundService;

    @PostMapping("/create")
    @Operation(summary = "创建退款")
    @PreAuthorize("@ss.hasPermission('member:user-refund:create')")
    public CommonResult<Long> createUserRefund(@Valid @RequestBody UserRefundSaveReqVO createReqVO) {
        createReqVO.setStatus(RefundStatusEnum.WAITING.getCode());
        return success(userRefundService.createUserRefund(createReqVO));
    }

//    /**
//     * 退款编辑
//     *
//     * @param id
//     * @param files
//     * @param status
//     * @param remark
//     * @return
//     */
//    @PostMapping("/update")
//    @Operation(summary = "退款编辑")
//    public CommonResult<Boolean> update(
//            @Parameter(description = "退款id") @RequestParam(name = "id", required = false) Long id,
//            @Parameter(description = "上传的图片列表（最多1个图片文件）") @RequestParam(name = "files", required = false) MultipartFile[] files,
//            @Parameter(description = "退款状态") @RequestParam(name = "status", required = false) Integer status,
//            @Parameter(description = "退款方式") @RequestParam(name = "type", required = false) Integer type,
//            @Parameter(description = "联系方式") @RequestParam(name = "contact", required = false) String contact,
//            @Parameter(description = "备注") @RequestParam(name = "remark", required = false) String remark) {
//        return success(userRefundService.updateUserRefund(id, files, status, type, contact, remark));
//    }

    @PostMapping("/update")
    @Operation(summary = "更新退款")
    @PreAuthorize("@ss.hasPermission('member:user-refund:update')")
    public CommonResult<Boolean> updateUserRefund(@Valid @RequestBody UserRefundUpdateReqVO updateReqVO) {
        return success(userRefundService.updateUserRefund(updateReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除退款")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-refund:delete')")
    public CommonResult<Boolean> deleteUserRefund(@RequestParam("id") Long id) {
        userRefundService.deleteUserRefund(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得退款")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-refund:query')")
    public CommonResult<UserRefundRespVO> getUserRefund(@RequestParam("id") Long id) {
        UserRefundDO userRefund = userRefundService.getUserRefund(id);
        return success(BeanUtils.toBean(userRefund, UserRefundRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得退款分页")
    @PreAuthorize("@ss.hasPermission('member:user-refund:query')")
    public CommonResult<PageResult<UserRefundRespVO>> getUserRefundPage(@Valid @RequestBody UserRefundPageReqVO pageReqVO) {
        return success(userRefundService.getUserRefundPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出退款 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-refund:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserRefundExcel(@Valid UserRefundPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserRefundRespVO> list = userRefundService.getUserRefundPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "退款.xls", "数据", UserRefundRespVO.class, list);
    }

}